var URL_ADDHOTSPOT="/api/ADDHOTSPOT"
var URL_NEARBYHOTSPOT="/api/NEARBYHOTSPOT"
var map;
var coder,search;
$(document).ready(function(){
	initMap();
});

function mark(geocodes){
	console.debug(geocodes)
	for(var i=0;i<geocodes.length;i++){
		var item=geocodes[i];
		lnglat=[item.location.lng,item.location.lat];
		var marker=new AMap.Marker({position:lnglat,map:map})
		// map.addEventListener(marker,'click',markerClick)
		AMap.event.addListener(marker,'click',markerClick)
	}
}
function searchCallBack(state,data){
	if(state!="complete"||data.info!='OK'){
		console.debug("Failed to search")
		return;
	}
	// var markers=new Array(data.poiList.pois.length)
	mark(data.poiList.pois)
	/*for (var i = 0; i < data.poiList.pois.length; i++) {
		var item=data.poiList.pois[i]
		console.debug(JSON.stringify(item))
	}*/
}
// circle={
// 	lat:0,
// 	lng:0,
// 	radius:0
// }
function markerClick(event) {
	console.debug(event)
	map.setCenter(event.lnglat)
	var circle={
		lat:event.lnglat.lat,
		lng:event.lnglat.lng,
		radius:1000
	}
	nearbyHotspot(circle)
}
function initMap(){
	map=new AMap.Map("container");
	map.setZoom(10);
	map.setCity("0371");
	AMap.event.addListener(map, "click", _onClick);
	var geocoder;
	AMap.service('AMap.Geocoder',function(){
        geocoder = new AMap.Geocoder({
        });
  })
	AMap.service('AMap.PlaceSearch',function(){
		search= new AMap.PlaceSearch({ //构造地点查询类
        pageSize: 5,
        pageIndex: 1,
        city: "0371", //城市
      //  map: map,
    });
		AMap.plugin('AMap.ToolBar',function(){
			var toolbar = new AMap.ToolBar();
			map.addControl(toolbar)
	 })
	})
	coder=new geoEncode(geocoder)
	$("#searchipt").change(function(){
		map.clearMap()
		search.search($(this).val(),searchCallBack)
	})
}
 var _onClick = function(e){
        /*new AMap.Marker({
            position : e.lnglat,
            map : map
        })*/
		geoEncode(e.lnglat)
		coder.getAddress(e.lnglat)
    }

function geoEncode(coder){
	this.geocoder=coder;
	this.hotspot={
		tag:"",
		lat:0,
		lng:0,
	}
	this.getAddress=function(latlng){
		this.geocoder.getAddress(latlng,function(status,result){
		if (status === 'complete' && result.info === 'OK') {
			hotspot.tag=result.regeocode.formattedAddress
			hotspot.lat=latlng.lat;
			hotspot.lng=latlng.lng;
			console.debug(JSON.stringify(hotspot))
			addHotspot(hotspot)
		}else{
			console.debug("Failed to regeocode")
    }
	})
	}
	this.getLocation=function(addr,func){
		return this.geocoder.getLocation(addr,func);
	}
}

function addHotspot(hotspot){
	order={
		"command":"ADDHOTSPOT",
		"data":hotspot
	}
	data=JSON.stringify(order)
	console.debug(data)
	$.post(URL_ADDHOTSPOT,"data="+data,function(data,status){
		console.debug(JSON.stringify(data))
	})
}
function nearbyHotspot(circle){
	var order={
		'command':"NEARBYHOTSPOT",
		data:circle
	}
	var data="data="+JSON.stringify(order)
	$.post(URL_NEARBYHOTSPOT,data,function(result,state){
		console.debug(result)
		map.clearMap()
		markHotspots(result.data)
	})
}
function markHotspots(hotspots){
	for(key in hotspots){
		var marker=new AMap.Marker({position:[hotspots[key].lng,hotspots[key].lat],map:map})
		AMap.event.addListener(marker,'click',markerClick)
	}
}
